using System.Diagnostics;

//
// * Copyright (c) 2002-2009 "Neo Technology,"
// *     Network Engine for Objects in Lund AB [http://neotechnology.com]
// *
// * This file is part of Neo4j.
// * 
// * Neo4j is free software: you can redistribute it and/or modify
// * it under the terms of the GNU Affero General Public License as
// * published by the Free Software Foundation, either version 3 of the
// * License, or (at your option) any later version.
// * 
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// * GNU Affero General Public License for more details.
// * 
// * You should have received a copy of the GNU Affero General Public License
// * along with this program. If not, see <http://www.gnu.org/licenses/>.
// 
namespace org.neo4j.kernel.impl.nioneo.store
{


///
/// <summary> * Persistence window using a <seealso cref="MappedByteBuffer"/> as underlying buffer. </summary>
/// 
	internal class MappedPersistenceWindow : LockableWindow
	{
		private long position = -1;
		private Buffer buffer = null;
		private int windowSize = -1;
		private int recordSize = -1;
		private int totalSize = -1;

		internal MappedPersistenceWindow(long position, int recordSize, int totalSize, FileChannel channel, FileChannel.MapMode mapMode) : base(channel)
		{
			Debug.Assert(recordSize > 0, "Record size[" + recordSize + "] must be greater then zero");
			Debug.Assert(totalSize >= recordSize, "Total size[" + totalSize + "] cannot be less than record size[" + recordSize + "]");
			Debug.Assert(totalSize >= recordSize, "Total size[" + totalSize + "] must mod to zero with record size[" + recordSize + "]");
			this.totalSize = totalSize;
			windowSize = totalSize / recordSize;
			this.recordSize = recordSize;
			this.position = position;
			buffer = new Buffer(this);
			try
			{
				buffer.setByteBuffer(channel.map(mapMode, position * recordSize, totalSize));
			}
			catch (IOException e)
			{
				this.position = -1;
				throw new MappedMemException("Unable to map pos=" + position + " recordSize=" + recordSize + " totalSize=" + totalSize, e);
			}
		}

		public override Buffer getBuffer()
		{
			return buffer;
		}

		public virtual int getRecordSize()
		{
			return recordSize;
		}

		public override long position()
		{
			return position;
		}

		public override int size()
		{
			return windowSize;
		}

		public override void force()
		{
			((java.nio.MappedByteBuffer) buffer.getBuffer()).force();
		}

		public override bool Equals(object o)
		{
			if (!(o is MappedPersistenceWindow))
			{
				return false;
			}
			return position() == ((MappedPersistenceWindow) o).position();
		}

		internal virtual void unmap()
		{
			if (buffer != null)
			{
				((java.nio.MappedByteBuffer) buffer.getBuffer()).force();
				buffer.Close();
				position = -1;
			}
		}

		private volatile int hashCode = 0;

		public override int GetHashCode()
		{
			if (hashCode == 0)
			{
				hashCode = (int) position();
			}
			return hashCode;
		}

		public override string ToString()
		{
			return "MappedPersistenceWindow[p=" + position + ",rs=" + recordSize + ",ws=" + windowSize + ",ts=" + totalSize + "]";
		}

		public override void close()
		{
			buffer.Close();
		}

		public virtual Buffer getOffsettedBuffer(int id)
		{
			int offset = (int)((id & 0xFFFFFFFFL) - buffer.position()) * recordSize;
			buffer.setOffset(offset);
			return buffer;
		}
	}
}